<?php
/**
 * Created by PhpStorm.
 * User: ai
 * Date: 2017/11/28
 * Time: 14:44
 */

use kartik\detail\DetailView;
use backend\models\Country;
use yii\helpers\Url;
use yii\widgets\Pjax;
use backend\assets\KartikAsset;
use yii\helpers\Html;

KartikAsset::register($this);

$js = <<<JS
$(document).ready(function () {
    $("#project_file").fileinput({
            browseLabel:'选择文件',
            language:'zh',
            showBrowse:false,
            showCaption: false,
            dropZoneTitle:'点击或拖拽文件到这里',
            dropZoneClickTitle:'',           
            allowedFileExtensions: ["xls", "xlsx"],
            previewFileIconSettings: {
                'xls': '<i class="fa fa-file-excel-o text-success"></i>',              
            },
            previewFileExtSettings: {              
                'xls': function(ext) {
                    return ext.match(/(xls|xlsx)$/i);
                },              
            },
            browseOnZoneClick: true,
            uploadAsync: true,
            showUpload: true, 
            showRemove: true, 
            showUploadedThumbs: true,
            uploadUrl: $("#import-url").val(),
            deleteUrl: "",

        }).on('fileloaded', function(event, file, previewId, index, reader) {

        }).on('filebatchselected', function(event, files) {
            // console.log(files);
        })
    
  $('[data-toggle="tooltip"]').tooltip()
})
JS;

$this->registerJs($js);
echo '<input type="hidden" id="import-url" value="' . Url::to(['project-ease/import', 'package_id' => Yii::$app->request->get('id')]) . '">';

$modelCountry = new Country();
$arr_provinces = $modelCountry->getProvinces();
//if ($model->city) {
//    $arr_cities = $modelCountry->getProvinces("id in ($model->city)");
//} else {
//    $arr_cities = $modelCountry->getProvinces("pid = $model->province");
//}
$arr_cities = $modelCountry->getProvinces("pid = $model->province");


$province = implode('', $modelCountry->getProvinces("id =$model->province "));

if ($model->city) {
    $city = implode(',', $modelCountry->getProvinces("id in ($model->city)"));
    $model->city = explode(',', $model->city);
} else {
    $city = '';
}


//$js = <<<JS
//$(function() {
//  $("#transfer_form").on(
//  "pjax:end",function() {
//    $.pjax.reload({container:"#transfer_form"});
//  }
//  );
//});
//JS;
//$this->registerJs($js);

$js = <<<JS
$(function() {
  $("#packageease-principle_suit").blur(function() {
  var principal = $("#packageease-principal").val()
  var principal_suit = $(this).val()
  if(principal_suit>0 && principal>0){
      $("#packageease-percent_principle_suit").val((principal_suit/principal).toFixed(4))
  }
  });
  $("#packageease-principal").blur(function() {
  var principal = $(this).val()
  var principal_suit = $("#packageease-principle_suit").val()
  if(principal_suit>0 && principal>0){
      $("#packageease-percent_principle_suit").val((principal_suit/principal).toFixed(4))
  }
  var price=$("#packageease-price").val()
  if(price>0 && principal>0){
        $("#packageease-discount").val((price/principal).toFixed(4));
    }
  });
  
  $("#packageease-price").blur(function() {
    var price=$(this).val()
    var principal = $("#packageease-principal").val()
    if(price>0 && principal>0){
        $("#packageease-discount").val((price/principal).toFixed(4));
    }
  });
  
})
JS;
$this->registerJs($js);

if (($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']))) {
    ?>


    <div class="kv-panel-before">
        <div class="btn-toolbar kv-grid-toolbar toolbar-container pull-right">
            <div class="btn-group">
                <button type="button" class="btn btn-info" title="导入项目" data-toggle="modal"
                        data-target="#importExcelModal">导入项目
                </button>
                <?= Html::a('<i class="glyphicon glyphicon-file"></i>导入项目模板', ['project-ease/download-template'], ['class' => "btn btn-warning", 'title' => "下载导入项目模板"]) ?>
            </div>

        </div>

        <div class="clearfix"></div>
    </div>
<?php } ?>

<!-- Modal -->
<div class="modal fade" id="importExcelModal" tabindex="-1" role="dialog" aria-labelledby="importExcelModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="importExcelModalLabel">导入项目</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <?php //ActiveForm::begin(['action' => ['/project-ease/import'], 'method' => 'post', 'options' => ['enctype' => "multipart/form-data"]]) ?>
            <div class="modal-body">
                <div class="file-loading">
                    <input id="project_file" name="project_file" type="file"'>
                </div>
                <div id="kartik-file-errors"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal"
                        onclick="javascript:window.location.reload();">关闭
                </button>
                <?= Html::a('查看项目列表', ['project-ease/index'], ['class' => 'btn btn-info', 'target' => '_blank']) ?>
                <!--                <button type="submit" class="btn btn-primary">提交</button>-->
            </div>
            <?php //ActiveForm::end() ?>
        </div>
    </div>
</div>
<?php
Pjax::begin(['id' => 'basic_form']);
echo DetailView::widget([
    'model' => $model,
    'condensed' => true,
    'hover' => true,
    'mode' => 'view',
    'panel' => [
        'heading' => '基本信息',
        'headingOptions' => ['template' => '{title}'],
        'type' => DetailView::TYPE_INFO,
        'footer' => '<span class="glyphicon glyphicon-pencil"></span>',
        'footerOptions' => ['class' => 'panel-heading', 'tag' => 'h4', 'template' => '{buttons}{title}']
    ],
    'formOptions' => ['options' => ['data-pjax' => true],],
    'hideIfEmpty' => false,//如果有空字段则隐藏
    'buttons1' => '{update}',
    'buttons2' => '{view} {reset} {save}',
    'updateOptions' => ['label' => '<span class="btn btn-info">编辑</span>'],
    'viewOptions' => ['label' => '<span class="btn btn-info">查看</span>'],
    'saveOptions' => ['label' => ($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal'])) ? '<span class="btn btn-info">保存</span>' : ''],
    'resetOptions' => ['label' => '<span class="btn btn-info">重置</span>'],
    'attributes' => [
        ['attribute' => 'title', 'options' => ['placeholder' => '地区+持包人简称+户数+本金']],
        ['attribute' => 'type', 'type' => 'radioList', 'items' => Yii::$app->params['package_ease_type'], 'value' => Yii::$app->params['package_ease_type'][$model->type], 'options' => ['class' => 'form-inline']],
        ['attribute' => 'level', 'type' => 'radioList', 'items' => Yii::$app->params['package_ease_level'], 'value' => Yii::$app->params['package_ease_level'][$model->level], 'options' => ['class' => 'form-inline']],
        [
            'columns' => [
                [
                    'attribute' => 'province',
                    'type' => DetailView::INPUT_SELECT2,
                    'value' => $province,
                    'widgetOptions' => [
                        'data' => $arr_provinces,
                        'options' => ['placeholder' => '选择省份 ...'],
                        'pluginOptions' => [
                            'allowClear' => true
                        ]
                    ],
                    'valueColOptions' => ['style' => 'width:30%']
                ],
                [
                    'attribute' => 'city',
                    'type' => DetailView::INPUT_DEPDROP,
                    'widgetOptions' => [
                        'data' => count($arr_cities) > 0 ? $arr_cities : [],
                        'type' => 2,
                        'options' => ['multiple' => true],
                        'pluginOptions' => [
                            'tags' => true,
                            'tokenSeparators' => [',', ' '],
                            'depends' => ['packageease-province'],
                            'placeholder' => '选择城市...',
                            'url' => Url::to(['/country/get-city',]),
                        ]],
                    'value' => $city,
                ],
            ],
        ],
        'creditor',
        ['columns' => [['attribute' => 'principal', 'valueColOptions' => ['style' => 'width:30%'], 'format' => 'decimal'], 'principle_suit:decimal', 'percent_principle_suit',]],
        'interest:decimal',
        ['columns' => [['attribute' => 'hushu', 'valueColOptions' => ['style' => 'width:30%',]], 'hushu_suit',]],
        ['columns' => [['attribute' => 'price', 'format' => 'decimal', 'valueColOptions' => ['style' => 'width:30%', 'placeholder' => '当前卖方报价/预估售价']], 'discount',]],
        [
            'attribute' => 'highlights',
            'type' => 'textarea',
            'options' => ['placeholder' => '推介理由'],
        ],
        [
            'attribute' => 'total_mortgage_principal',
            'format' => 'decimal',
            'options' => ['placeholder' => '带抵押的项目抵押担保额之和']
        ],
        [
            'attribute' => 'other_note',
            'type' => 'textarea',
            'options' => ['placeholder' => '说明资产包沟通情况、初步结论等其它补充信息']
        ],

        [
            'attribute' => 'follow',
            'type' => 'textarea',
            'options' => ['placeholder' => '过会信息备注'],
        ],
        'published_at',
        'origin',
    ]
]);
Pjax::end();

if ($transferModel->transfer == '') {
    echo $this->render('_form_transfer', ['model' => $model, 'transferModel' => $transferModel]);
} elseif ($transferModel->transfer == '2') {
    echo $this->render('_form_transfer', ['model' => $model, 'transferModel' => $transferModel,]);
    echo $this->render('_form_transfer2', ['model' => $model, 'transferModel' => $transferModel, 'transferModel2' => $transferModel2,]);
} else {
    echo $this->render('_form_transfer', ['model' => $model, 'transferModel' => $transferModel ? $transferModel : $transferModel2,]);
}


?>
